// mircoApp/business-district/StaffMgmt/StaffMgmt.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    identity: "",
    finished: false,
    isLoadMore: true,
    current_page: 1,
    last_page: 1,

    activeOptions: {},
    pageTitle: '',
    keyword: "",
    addPop: false,
    member_id: '',
    search_member: {},

    info: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initPage();
    this.getData();
  },

  initPage() {
    this.pageOptions = {
      promoter: {
        placeholder: '搜索会员id/昵称',
        api: "plugin.business-district.frontend.controllers.agent.promoter-list",
        name: "推广者",
        search: {
          key: "promoter_member_id",
          api: "plugin.business-district.frontend.controllers.agent.check-promoter",
          add: 'plugin.business-district.frontend.controllers.agent.add-promoter'
        },
        switch: {
          api: "plugin.business-district.frontend.controllers.agent.operate-promoter",
          key: "id"
        }
      },
      clerk: {
        placeholder: '搜索会员id/昵称',
        api: "plugin.business-district.frontend.controllers.store.clerk-manage",
        name: "店员",
        search: {
          key: "clerk_member_id",
          api: "plugin.business-district.frontend.controllers.store.check-clerk",
          add: 'plugin.business-district.frontend.controllers.store.add-clerk'
        },
        switch: {
          api: "plugin.business-district.frontend.controllers.store.operate-clerk",
          key: "id"
        }
      },
      agentStore: {
        placeholder: '搜索门店名称',
        api: "plugin.business-district.frontend.controllers.agent.store-manage",
        name: "门店管理" //区域代理的门店管理
      },
      promoterStore: {
        placeholder: '搜索门店名称',
        api: "plugin.business-district.frontend.controllers.promoter.store-manage",
        name: "门店管理" //推广者的门店管理
      },
    };
    this.setData({
      activeOptions: this.pageOptions[this.options.identity],
      keyword: '',
      identity: this.options.identity,
      pageTitle: this.pageOptions[this.options.identity].name
    });
    wx.setNavigationBarTitle({
      title: this.data.activeOptions.name
    });
  },
  init(){
    this.current_page = 1;
    this.last_page = 1;
    this.data.isLoadMore = true;
    this.setData({
      finished: false,
      info: []
    });
  },
  async getData() {
    let json = { keyword: this.data.keyword };
    if(this.options.agent_id) json.agent_id = this.options.agent_id;
    let urlStr = app.getNetAddresss(this.data.activeOptions.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: json
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    let setdata = {};
    setdata.info = data.data || [];

    this.current_page = data.current_page;
    this.last_page = data.last_page;
    if (this.current_page == this.last_page) {
      setdata.finished = true;
    }
    this.setData(setdata);
  },
  onSearch(){
    this.init();
    this.getData();
  },

  inputChange()  {
    this.inputMember();
  },

  async inputMember(){
    let urlStr = app.getNetAddresss(this.data.activeOptions.search.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        [this.data.activeOptions.search.key]: this.data.member_id,
        agent_id: this.options.agent_id||''
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    this.setData({
      search_member: data
    });
  },
  async addMember(){
    if(!this.data.member_id) return app('请输入会员ID');
    let urlStr = app.getNetAddresss(this.data.activeOptions.search.add);
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        [this.data.activeOptions.search.key]: this.data.member_id,
        agent_id: this.options.agent_id||''
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    this.setData({
      addPop: false
    });
    this.onSearch();
  },
  async changeStatus(e){
    let id = e.currentTarget.dataset.id;
    let urlStr = app.getNetAddresss(this.data.activeOptions.switch.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        [this.data.activeOptions.switch.key]: id,
        agent_id: this.options.agent_id||''
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    this.onSearch();
  },
  onClickVal(e){
    let {name,val} = e.currentTarget.dataset;
    this.setData({
      [name]: val
    });
  },

  async getMoreData(){
    if (this.current_page >= this.last_page) {
      return;
    }
    this.data.isLoadMore = false;
    this.current_page = this.current_pag + 1; 
    let json = { 
      keyword: this.data.keyword,
      page: this.current_page
    };
    if(this.options.agent_id) json.agent_id = this.options.agent_id;

    let urlStr = app.getNetAddresss(this.data.activeOptions.api);
    let response = await app._postNetWork({
      url: urlStr,
      data: json,
    });
    let { result, msg, data } = response.data;
    this.data.isLoadMore = true;
    if(result == 0) return app.tips(msg);
    this.setData({
      info: this.data.info.concat(data.data)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage() {

  // }
})